package com.bj58.mis.apm.server.statistics.controller;

import com.bj58.mis.apm.server.core.common.result.ResultBean;
import com.bj58.mis.apm.server.statistics.dto.common.Pair;
import com.bj58.mis.apm.server.statistics.dto.common.User;
import com.bj58.mis.apm.server.statistics.service.CommonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 通用基础Controller
 */
@RestController
@RequestMapping("/common")
@Api(tags = "Common相关API", description = "/common")
public class CommonController extends BaseController<CommonService> {

    /**
     * 查询当前用户有权限的项目列表，管理员查看全部
     * @param owner
     * @return
     */
    @ApiOperation(value = "项目列表", notes = "查询当前用户有权限的项目列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="owner", value = "负责人", paramType = "query", required = false, dataType = "String")
    })
    @GetMapping(path="/projects")
    public ResultBean<List<Pair>> queryProjects(@RequestParam(name="owner", required = false) String owner,@RequestParam(name="removeAll", required = false) String removeAll){
        List<Pair> list = this.service.queryProjects(owner, getLoginUserName());
        if(!"true".equals(removeAll)) {
            Pair p = new Pair();
            p.setLabel("全部");
            p.setValue("");
            list.add(0, p);
        }
        return ResultBean.success(list);
    }

    /**
     * 查询当前用户有权限的Cube列表，管理员查看全部
     * @param projectId
     * @param owner
     * @return
     */
    @ApiOperation(value = "Cube列表", notes = "查询当前用户有权限的Cube列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="projectId", value = "项目ID", paramType = "query", required = false, dataType = "String"),
            @ApiImplicitParam(name="owner", value = "负责人", paramType = "query", required = false, dataType = "String")
    })
    @GetMapping(path="/cubes")
    public ResultBean<List<Pair>> queryCubes(@RequestParam(name="projectId", required = false) String projectId,
                                                 @RequestParam(name="owner", required = false) String owner,
                                             @RequestParam(name="removeAll", required = false) String removeAll){
        if(StringUtils.isEmpty(projectId)){
            return ResultBean.success(new ArrayList<>());
        }
        List<Pair> list = this.service.queryCubes(projectId, owner, getLoginUserName());
        if(!"true".equals(removeAll)) {
            Pair p = new Pair();
            p.setLabel("全部");
            p.setValue("");
            list.add(0, p);
        }
        return ResultBean.success(list);
    }

    /**
     * 查询负责人列表
     * @param projectId
     * @return
     */
    @ApiOperation(value = "负责人列表", notes = "查询负责人列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="projectId", value = "项目ID", paramType = "query", required = false, dataType = "String"),
            @ApiImplicitParam(name="keyword", value = "输入框输入内容", paramType = "query", required = false, dataType = "String")
    })
    @GetMapping(path="/owners")
    public ResultBean<List<Pair>> qeuryOwners(@RequestParam(name="projectId", required = false) String projectId, @RequestParam(name="keyword", required = false) String keyword){
        return ResultBean.success(this.service.qeuryOwners(projectId, keyword, getLoginUserName()));
    }

    @ApiOperation(value = "登陆用户信息", notes = "查询登陆用户信息")
    @GetMapping(path="/currentUser")
    public ResultBean<User> getCurrentUser(){
        return ResultBean.success(this.service.getCurrentUser(getLoginUserName()));
    }
}
